package com.suirich.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.suirich.bean.Enquiry;
import com.suirich.common.Page;
import com.suirich.dao.EnquiryDao;

@Repository
public class EnquiryDaoImpl extends GenericDaoHibernateImpl<Enquiry, Long> implements EnquiryDao {
	public EnquiryDaoImpl() {
		super(Enquiry.class);
	}

	@SuppressWarnings("unchecked")
	public List<Enquiry> getEnquiryList(Page page) {
		String hql = "select e.id as id,e.title as title,e.companyName as companyName," +
				"e.contactPerson as contactPerson,e.tel as tel,e.email as email,e.createDate as createDate " +
				"from Enquiry e order by e.createDate desc";
		
		Query query = this.getCurrentSession().createQuery(hql)
				.setResultTransformer(Transformers.aliasToBean(Enquiry.class));

        if (page != null) {
            query.setFirstResult((page.getCurrentPage() - 1)
                    * page.getPerPageSize());
            query.setMaxResults(page.getPerPageSize());
        }
        
		return (List<Enquiry>)query.list();
	}

	public Long getEnquiryListCount() {
		String hql = "select count(e.id) from Enquiry e";
		
		Query query = this.getCurrentSession().createQuery(hql);
		
		return (Long)query.uniqueResult();
	}
}
